package com.lab.webSocket.service.impl;

import com.lab.webSocket.dto.CheckUserIdReturnDto;
import com.lab.webSocket.dto.UserDto;
import com.lab.webSocket.model.UserEntity;
import com.lab.webSocket.repository.UserRepository;
import com.lab.webSocket.service.LoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by xu_jiaHui on 2017/8/26.
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private UserRepository userRepository;

    public UserDto loginCheck(String userId, String userPsw) throws Exception {
        UserDto userDto= userRepository.loginCheck(userId,userPsw);
        System.out.println(userDto.toString());
        ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 登录成功后设置Session属性用于验证
        if (userDto.getLoginResult() == 200) {
            HttpSession wsUserLoginSession = request.getSession();
            wsUserLoginSession.setAttribute("wsUserLoginSession", userDto);
        }
        return userDto;
    }

    public Integer userRegister(UserDto userDto) throws Exception {
        System.out.println(userDto.toString());
        return userRepository.save(userDto.toBean()).getUserId();
    }

    public CheckUserIdReturnDto checkUserId(String userId) throws Exception {
        System.out.println(userId);
        return userRepository.checkUserId(userId);
    }
}
